﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TripleGameDAL;

namespace WebApplication2
{
    public partial class Game1 : System.Web.UI.Page
    {
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                int gameId = int.Parse(Request.QueryString["id"]);
                var currentGame = TripleGameDALL.GetGameByID(gameId);
                detailsViewCurrentGame.DataSource = new List<Game> { currentGame };
                detailsViewCurrentGame.DataBind();
                //p1 = new Player();
                //p1.Name = games[gameId - 1].PlayerOne;
                //p1.Sign = "x";
                //Application["p1"] = p1;
            }
            if (Session["state"] == null)
            {
                char[] state = new char[50];
                for (int i = 0; i < 50; i++)
                {
                    state[i] = 'o';
                }
                Session["state"] = state;
            }
            DrawBoard();
        }

        protected string GetUserName(string property)
        {
            int? userID = (int?)Eval(property);
            if (userID == null)
            {
                return "Empty";

            }
            User u = TripleGameDALL.GetUserByID(userID);
            return u.Name;
        }

        protected void timerUpdatePlayerDetails_Tick(object sender, EventArgs e)
        {
            int gameId = int.Parse(Request.QueryString["id"]);
            var currentGame = TripleGameDALL.GetGameByID(gameId);
            if (currentGame.User2ID != null)
            {
                timerUpdatePlayerDetails.Enabled = false;
                StartGame();
            }
            detailsViewCurrentGame.DataSource = new List<Game> { currentGame };
            detailsViewCurrentGame.DataBind();
        }

        private void StartGame()
        {
            this.gameTable.Visible = true;
            this.timerGame.Enabled = true;
        }


        protected void timerGame_Tick(object sender, EventArgs e)
        {
            this.Label1.Text = DateTime.Now.ToLongTimeString();
        }

        protected void ImageButton_Click(object sender, CommandEventArgs e)
        {
            int buttonID = int.Parse(e.CommandArgument.ToString());
            string id = (buttonID - 1).ToString();
            char[] s = (char[])Session["state"];
            s[buttonID - 1] = 'a';
            Session["state"] = s;
            ((ImageButton)upGameTable.ContentTemplateContainer.FindControl(id)).ImageUrl = "~/Styles/images/cellA.png";
        }

        private void DrawBoard()
        {
            char[] s = (char[])Session["state"];
            for (int i = 1; i <= 50; i++)
            {
                ImageButton imgButton = new ImageButton();
                imgButton.ID = id.ToString();
                id++;
                if (s[i - 1] == 'o')
                {
                    imgButton.ImageUrl = "~/Styles/images/empty.png";
                }
                else if (s[i - 1] == 'a')
                {
                    imgButton.ImageUrl = "~/Styles/images/cellA.png";
                }
                imgButton.CommandArgument = id.ToString();
                imgButton.Command += new CommandEventHandler(ImageButton_Click);
                imgButton.Width = 40;
                gameTable.Controls.Add(imgButton);

                if ((i % 10 == 0) && (i > 0))
                {
                    Label breakLabel = new Label();
                    breakLabel.Text = "<br />";
                    gameTable.Controls.Add(breakLabel);
                }
            }
        }
    }
}